﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using XQ.Framework.io.text;
using XQ.WeChatOfficialAccount.BLL.Sys;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.WebAPI.Filter;
using XQ.WeChatOfficialAccount.WebAPI.Models;

namespace XQ.WeChatOfficialAccount.WebAPI.Controllers.System
{
    /// <summary>
    /// 字典值操作
    /// </summary>
    //[FormAuthenticationFilter]
    public class DictController : ApiController
    {
        /// <summary>
        /// 获取字典
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Dict/GetDictList")]
        public IEnumerable<DictModel> GetDictList(int categoryId)
        {
            DictBLL bll = new DictBLL();
            List<DictModel> dictModels = bll.GetDictList(categoryId);
            //dictModels.Insert(0,new DictModel
            //{
            //    ID=-1,
            //    Title = "请选择"
            //});
            return dictModels;
        }

        /// <summary>
        /// 获取字典
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Dict/GetDictListByCategoryId")]
        public ResultModel GetDictListByCategoryId(int categoryId)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                DictBLL bll = new DictBLL();
                resultModel.success = true;
                resultModel.code = "10000";
                resultModel.message = "操作成功";
                resultModel.data = bll.GetDictList(categoryId);
                return resultModel;
            }
            catch (Exception ex)
            {
                resultModel.success = false;
                resultModel.code = "10001";
                resultModel.message = ex.Message;
                return resultModel;
            }
        }

        /// <summary>
        /// 根据字典分类Code获取当前分类的字典列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Dict/GetDictByCode")]
        public IEnumerable<DictModel> GetDictByCode(string categoryCode)
        {
            DictBLL bll = new DictBLL();
            List<DictModel> dictModels = bll.GetDictByCode(categoryCode);
            dictModels.Insert(0, new DictModel
            {
                ID = -1,
                Title = "请选择"
            });
            return dictModels;
        }


        /// <summary>
        /// 根据ID获取字典
        /// </summary>
        /// <returns></returns>
        [Route("Api/Dict/GetDictById")]
        public Sys_Dics GetDictById(int dictId)
        {
            DictBLL bll = new DictBLL();
            return bll.GetDictById(dictId);
        }

        /// <summary>
        /// 字典项保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("Api/Dict/SaveDict")]
        public string SaveDict(Sys_Dics model)
        {
            try
            {
                DictBLL bll = new DictBLL();
                if (bll.SaveDict(model))
                {
                    return "ok";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return "ok";
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Dict/DeleteDict")]
        public string DeleteDict(int dictId)
        {
            DictBLL bll = new DictBLL();
            if (bll.DeleteDict(dictId))
            {
                return "success";
            }
            return "err";
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("Api/Dict/DelDicts")]
        public bool DelDicts(List<int> dictIds)
        {
            DictBLL bll = new DictBLL();
            return bll.DelDicts(dictIds);
        }

        [Route("Api/Dict/GetDictCategoryList")]
        public IEnumerable<Tags> GetDictCategoryList()
        {
            DictCategoryBll dcBll = new DictCategoryBll();
            return dcBll.GetDictCategoryListForTags();
        }
        /// <summary>
        /// 获取下拉框的值
        /// </summary>
        /// <param name="CategoryId"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [Route("Api/Dict/GetDictValue")]
        public ResultModel GetDictValue(int? CategoryId)
        {
            ResultModel resultModel = new ResultModel();
            List<Sys_Dics> menus = new List<Sys_Dics>();
            try
            {
                DictBLL bll = new DictBLL();
                menus = bll.GetDictValue(CategoryId);
                resultModel.success = true;
                resultModel.code = "10000";
                resultModel.message = "执行成功";
                resultModel.data = menus;
            }
            catch (Exception ex)
            {
                resultModel.success = false;
                resultModel.code = "10001";
                resultModel.message = "执行失败";
                resultModel.data = ex.Message;
            }

            return resultModel;
        }
    }
}